package org.edu.modules.jxzy.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.regexp.internal.RE;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxzy.entity.JwJiaocaiKucun;
import org.edu.modules.jxzy.service.IJwCangkuService;
import org.edu.modules.jxzy.service.IJwJiaocaiKucunService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: 教材库存表
 * @Author: jeecg-boot
 * @Date:   2021-04-14
 * @Version: V1.0
 */
@Api(tags="教材库存表")
@RestController
@RequestMapping("/jxzy/jwJiaocaiKucun")
@Slf4j
public class JwJiaocaiKucunController extends BaseController<JwJiaocaiKucun, IJwJiaocaiKucunService> {
	@Autowired
	private IJwJiaocaiKucunService jwJiaocaiKucunService;

	@Autowired
	private IJwCangkuService jwCangkuService;
	
	/**
	 * 分页列表查询
	 *
	 * @param jwJiaocaiKucun
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "教材库存表-分页列表查询")
	@ApiOperation(value="教材库存表-分页列表查询", notes="教材库存表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(JwJiaocaiKucun jwJiaocaiKucun,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JwJiaocaiKucun> queryWrapper = QueryGenerator.initQueryWrapper(jwJiaocaiKucun, req.getParameterMap());

		Page<JwJiaocaiKucun> page = new Page<JwJiaocaiKucun>(pageNo, pageSize);
		IPage<JwJiaocaiKucun> pageList = jwJiaocaiKucunService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param jwJiaocaiKucun
	 * @return
	 */
	@AutoLog(value = "教材库存表-添加")
	@ApiOperation(value="教材库存表-添加", notes="教材库存表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JwJiaocaiKucun jwJiaocaiKucun) {

		//获取秒级别的时间戳
		long l = jwJiaocaiKucun.getTime().getTime() / 1000;
		jwJiaocaiKucun.setUpdatetime(new Long(l).intValue());

		jwJiaocaiKucunService.save(jwJiaocaiKucun);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jwJiaocaiKucun
	 * @return
	 */
	@AutoLog(value = "教材库存表-编辑")
	@ApiOperation(value="教材库存表-编辑", notes="教材库存表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody JwJiaocaiKucun jwJiaocaiKucun) {
		jwJiaocaiKucunService.updateById(jwJiaocaiKucun);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教材库存表-通过id删除")
	@ApiOperation(value="教材库存表-通过id删除", notes="教材库存表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		jwJiaocaiKucunService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "教材库存表-批量删除")
	@ApiOperation(value="教材库存表-批量删除", notes="教材库存表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jwJiaocaiKucunService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教材库存表-通过id查询")
	@ApiOperation(value="教材库存表-通过id查询", notes="教材库存表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		JwJiaocaiKucun jwJiaocaiKucun = jwJiaocaiKucunService.getById(id);
		if(jwJiaocaiKucun==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(jwJiaocaiKucun);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jwJiaocaiKucun
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JwJiaocaiKucun jwJiaocaiKucun) {
        return super.exportXls(request, jwJiaocaiKucun, JwJiaocaiKucun.class, "教材库存表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JwJiaocaiKucun.class);
    }


	 /**
	  * 通过jcid查询
	  *
	  * @param jcid
	  * @return
	  */
	 @AutoLog(value = "教材库存表-通过jcid查询")
	 @ApiOperation(value="教材库存表-通过id查询", notes="教材库存表-通过id查询")
	 @GetMapping(value = "/queryByJcid")
	 public Result<?> queryByJcid(@RequestParam(name="jcid",required=true) Integer jcid) {
		 List<JwJiaocaiKucun> kucuns = jwJiaocaiKucunService.getByJcid(jcid);
		 if(kucuns==null||kucuns.size()==0) {
			 return Result.error("此教材库存为空");
		 }

		 //仓库名称赋值
		 for(JwJiaocaiKucun item:kucuns){
			item.setCkmc(jwCangkuService.getById(item.getCkid()).getName());
		 }
		 return Result.ok(kucuns);
	 }

	 /**
	  * 出库操作
	  *
	  * @param jwJiaocaiKucun
	  * @return
	  */
	 @AutoLog(value = "教材库存表-出库操作")
	 @ApiOperation(value="教材库存表-出库操作", notes="教材库存表-出库操作")
	 @PostMapping(value = "/putout")
	 public Result<?> putout(@RequestBody JwJiaocaiKucun jwJiaocaiKucun) {

	 	if(jwJiaocaiKucun.getCkid()==null||jwJiaocaiKucun.getJcid()==null||jwJiaocaiKucun.getNum()==null){
	 		return Result.error("请求参数有误");
		}
		 List<JwJiaocaiKucun> kucuns = jwJiaocaiKucunService.getByJcid(jwJiaocaiKucun.getJcid());
		 if(kucuns==null||kucuns.size()==0) {
			 return Result.error("此教材库存为空，不得进行出库操作");
		 }
		 List<Integer> kucunIdS = kucuns.stream().map(item -> item.getCkid()).collect(Collectors.toList());
		 if(!kucunIdS.contains(jwJiaocaiKucun.getCkid())){
		 	return Result.error("此教材的库存里不存在这个仓库id");
		 }

		 //获取出库信息
		 JwJiaocaiKucun info = jwJiaocaiKucunService.getBy2id(jwJiaocaiKucun.getJcid(), jwJiaocaiKucun.getCkid());

		 Integer num = info.getNum();
		 num-=jwJiaocaiKucun.getNum();

		 if(num<0){
			return Result.error("出库数量不得大于库存数量");
		 }
		 //当库存数为零时直接删除数据
		 if(num==0){
		 	jwJiaocaiKucunService.removeById(info.getId());
		 }
		 info.setNum(num);

		 //获取秒级别的时间戳
		 long l = jwJiaocaiKucun.getTime().getTime() / 1000;
		 jwJiaocaiKucun.setUpdatetime(new Long(l).intValue());


		 jwJiaocaiKucunService.updateById(info);
		 return Result.ok("出库成功");
	 }



	 /**
	  * 入库操作
	  *
	  * @param jwJiaocaiKucun
	  * @return
	  */
	 @AutoLog(value = "教材库存表-入库操作")
	 @ApiOperation(value="教材库存表-入库操作", notes="教材库存表-入库操作")
	 @PostMapping(value = "/putin")
	 public Result<?> putin(@RequestBody JwJiaocaiKucun jwJiaocaiKucun) {

		 if(jwJiaocaiKucun.getCkid()==null||jwJiaocaiKucun.getJcid()==null||jwJiaocaiKucun.getNum()==null){
			 return Result.error("请求参数有误");
		 }
		 List<JwJiaocaiKucun> kucuns = jwJiaocaiKucunService.getByJcid(jwJiaocaiKucun.getJcid());

		 List<Integer> kucunIdS = kucuns.stream().map(item -> item.getCkid()).collect(Collectors.toList());

		 //数据库中是否存在的库存信息
		 if(!kucunIdS.contains(jwJiaocaiKucun.getCkid())){
			 //直接加入
			 jwJiaocaiKucunService.save(jwJiaocaiKucun);
			 return Result.ok("入库成功");
		 }

		 //获取库存信息
		 JwJiaocaiKucun info = jwJiaocaiKucunService.getBy2id(jwJiaocaiKucun.getJcid(), jwJiaocaiKucun.getCkid());

		 Integer num = info.getNum();
		 //数量累加
		 num+=jwJiaocaiKucun.getNum();
		 info.setNum(num);


		 //获取秒级别的时间戳
		 long l = jwJiaocaiKucun.getTime().getTime() / 1000;
		 jwJiaocaiKucun.setUpdatetime(new Long(l).intValue());


		 jwJiaocaiKucunService.updateById(info);
		 return Result.ok("入库成功");
	 }




}
